﻿namespace UI
{
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using Common.Localization;
    using Common.Protocol;

    public enum CellType { Active, Available }

    public partial class Cell : UserControl, ILocalizable
    {
        private Channel channel;

        private string value;
        
        #region constructors

        public Cell()
        {
            this.InitializeComponent();

            this.Color = Color.Purple;
            Cell.Height = base.Height = 85;
            Cell.Width = base.Width = 187;
            Cell.Margin = 1;
        }

        static Cell()
        {
            Cell.Height = 85;
            Cell.Width = 187;
            Cell.Margin = 1;
        }

        #endregion

        public event EventHandler OptionsClicked;
        
        #region public properties

        public Color Color
        {
            get
            {
                return this.colorPanel.BackColor;
            }

            set
            {
                this.colorPanel.BackColor = value;
                this.Invalidate();
            }
        }

        public string Value
        {
            get
            {
                return this.valueLabel.Text;
            }

            set
            {
                this.valueLabel.Text = value;
                this.valueLabel.Left = base.Width - this.valueLabel.Width - 5;
            }
        }

        public string HeaderText
        {
            get
            {
                return this.headerLabel.Text;
            }

            set
            {
                this.headerLabel.Text = value;
            }
        }

        public static int Height { get; private set; }

        public static int Width { get; private set; }

        public static int Margin { get; private set; }
        
        #endregion

        public void SetChannelValue(DateTime currentTime)
        {
            this.value = this.channel.GetValue(currentTime);
            this.Value = LocalizationProvider.GetMessage(value);
        }

        public void Initialize(Channel channel)
        {
            this.Color = channel.Color;
            this.HeaderText = channel.Id + " " + channel.Sensor + " " + channel.Min + ".." + channel.Max + channel.ValueUnits;
            this.valueUnitsLabel.Text = channel.ValueUnits;
            this.valueUnitsLabel.Left = base.Width - this.valueUnitsLabel.Width - 3;
            this.nameLabel.Text = channel.Name;

            this.channel = channel;
        }

        private void CellPaint(object sender, PaintEventArgs e)
        {
            Rectangle border = new Rectangle(0, 0, base.Width - 1, base.Height - 1);
            e.Graphics.DrawRectangle(new Pen(this.Color), border);
        }
        
        public void Localize()
        {
            this.optionsLinkLabel.Text = LocalizationProvider.GetMessage("options");
            this.Value = LocalizationProvider.GetMessage(this.value);
        }

        private void OptionsLinkClickedEventHandler(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (this.OptionsClicked != null)
            {
                this.OptionsClicked(this, null);
            }
        }
    }
}